﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using com.cgWorkstudio.BIMP.Library.Properties;
using com.cgWorkstudio.BIMP.Library.vo;
using com.cgWorkstudio.BIMP.Library.util;

namespace com.cgWorkstudio.BIMP.Library.uc {
    public partial class UC_DataGridView:DataGridView {
        CGDataGridViewColumn Column1;
        CGDataGridViewColumn Column2;
        CGDataGridViewColumn Column3;
        CGDataGridViewColumn Column4;
        CGDataGridViewColumn Column5;
        CGDataGridViewColumn Column6;
        /// <summary>
        /// UC_DataGridView
        /// </summary>
        public UC_DataGridView() {
            this.RowHeadersVisible = false;
            this.ColumnHeadersVisible = false;
            this.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.DefaultCellStyle.SelectionBackColor = Color.White;
            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint,true);
            UpdateStyles();

            this.AllowUserToAddRows = false;
            this.AllowUserToDeleteRows = false;
            this.AllowUserToResizeColumns = false;
            this.AllowUserToResizeRows = false;

            this.CellMouseDown += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseDown);
            this.CellMouseMove +=new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseMove);
            this.CellMouseLeave +=new DataGridViewCellEventHandler(dataGridView1_CellMouseLeave);
            this.CellMouseUp += new DataGridViewCellMouseEventHandler(dataGridView1_CellMouseUp);
            this.CellMouseClick += new DataGridViewCellMouseEventHandler(UC_DataGridView_CellMouseClick);

            this.Column1 = new CGDataGridViewColumn();
            this.Column2 = new CGDataGridViewColumn();
            this.Column3 = new CGDataGridViewColumn();
            this.Column4 = new CGDataGridViewColumn();
            this.Column5 = new CGDataGridViewColumn();
            this.Column6 = new CGDataGridViewColumn();
            // 
            // Column1
            // 
            this.Column1.HeaderText = "Column1";
            this.Column1.Image = Resources.Pic_DefaultMenuItem;
            this.Column1.Name = "Column1";
            // 
            // Column2
            // 
            this.Column2.HeaderText = "Column2";
            this.Column2.Image = Resources.Pic_DefaultMenuItem;
            this.Column2.Name = "Column2";
            // 
            // Column3
            // 
            this.Column3.HeaderText = "Column3";
            this.Column3.Image = Resources.Pic_DefaultMenuItem;
            this.Column3.Name = "Column3";
            // 
            // Column4
            // 
            this.Column4.HeaderText = "Column4";
            this.Column4.Image = Resources.Pic_DefaultMenuItem;
            this.Column4.Name = "Column4";
            // 
            // Column5
            // 
            this.Column5.HeaderText = "Column5";
            this.Column5.Image = Resources.Pic_DefaultMenuItem;
            this.Column5.Name = "Column5";
            // 
            // Column6
            // 
            this.Column6.HeaderText = "Column6";
            this.Column6.Image = Resources.Pic_DefaultMenuItem;
            this.Column6.Name = "Column6";

            this.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Column1,
            this.Column2,
            this.Column3,
            this.Column4,
            this.Column5,
            this.Column6});
        }

        void UC_DataGridView_CellMouseClick(object sender,DataGridViewCellMouseEventArgs e) {
            OnCGMenuButtonClicked(CGToolkit.IngoreNull(this[e.ColumnIndex,e.RowIndex].Tag),sender,e);
        }
        int nRow = 0;
        int nCell = 0;
        bool mouseMoving = false;
        public delegate void OnCGMenuButtonClickedCallBack(string buttonFlag,object sender,DataGridViewCellMouseEventArgs e);
        public event OnCGMenuButtonClickedCallBack OnCGMenuButtonClicked;
        private void dataGridView1_CellMouseMove(object sender,DataGridViewCellMouseEventArgs e) {
            if(mouseMoving == true) {
                return;
            }

            if(this[e.ColumnIndex,e.RowIndex].Tag == null) {    // 忽略空列
                this[e.ColumnIndex,e.RowIndex].Style.SelectionBackColor = Color.White;
                this[nCell,nRow].Style.SelectionBackColor = Color.White;
                return;
            } else {
                mouseMoving = true;
                this[e.ColumnIndex,e.RowIndex].Style.BackColor = Color.FromArgb(178,213,237);
                nRow = e.RowIndex;
                nCell = e.ColumnIndex;
            }
        }

        private void dataGridView1_CellMouseLeave(object sender,DataGridViewCellEventArgs e) {
            if(this[e.ColumnIndex,e.RowIndex].Tag == null) {    // 忽略空列
                this[e.ColumnIndex,e.RowIndex].Style.SelectionBackColor = Color.White;
                this[nCell,nRow].Style.SelectionBackColor = Color.White;
                return;
            } else {
                mouseMoving = false;
                this[e.ColumnIndex,e.RowIndex].Style.BackColor = Color.White;
            }

        }

        private void dataGridView1_CellMouseDown(object sender,DataGridViewCellMouseEventArgs e) {
            if(this[e.ColumnIndex,e.RowIndex].Tag == null) {    // 忽略空列
                this[e.ColumnIndex,e.RowIndex].Style.SelectionBackColor = Color.White;
                this[nCell,nRow].Style.SelectionBackColor = Color.White;
                return;
            } else {
                mouseMoving = true;
                this[nCell,nRow].Style.Padding = new Padding(0,1,0,0);
                this[nCell,nRow].Style.SelectionBackColor = Color.FromArgb(161,196,243);
            }
        }

        private void dataGridView1_CellMouseUp(object sender,DataGridViewCellMouseEventArgs e) {
            if(this[e.ColumnIndex,e.RowIndex].Tag == null) {    // 忽略空列
                this[e.ColumnIndex,e.RowIndex].Style.SelectionBackColor = Color.White;
                this[nCell,nRow].Style.SelectionBackColor = Color.White;
                return;
            } else {
                mouseMoving = true;
                this[nCell,nRow].Style.Padding = new Padding(0);
                this[nCell,nRow].Style.SelectionBackColor = Color.FromArgb(161,196,243);
            }
        }
    }
}
